<?php
/**
 * 作者: 王志昂-黑米<wangzhiangtony@qq.com>
 * 个人说明: 春水初涨，春林初盛，春风十里，不如你。
 * 创建时间: 2015-08-19 16:18
 * 说明:
 */
namespace Home\Service;

use \Think\Model;
use \Think\Log;

class DingdingService extends Model
{
    private $_url = 'https://oapi.dingtalk.com/gettoken?corpid=ding015c49072e39e4e635c2f4657eb6378f&corpsecret=aL0flqLcC-wbslUdXEfZMOQtiKoVenhPLB6gYkmsAlzYCCK0h15wVdilzr9BbxRB';

    public function sendTextMsg($touser, $text, $agentid = 153942749)
    {
        $accessToken = $this->_getAccessToken();
        if ($accessToken == null) {
            return '获取accessToken失败';
        }
        $content = array(
            'touser'  => $touser,
            'agentid' => $agentid,
            'msgtype' => 'text',
            'text'    => array('content' => $text)
        );
        $url     = 'https://oapi.dingtalk.com/message/send?access_token=' . $accessToken;
        $result  = $this->_sendPost($url, $content);
        Log::init(array(
            'log_path' => C('LOG_PATH')
        ));
        Log::write('向userid为' . $touser . '发送消息');
        Log::write($result);
        return $result;
    }

    private function _getAccessToken()
    {
        $content = $this->_sendGet($this->_url);
        $json    = json_decode($content, true);
        if ($json['errcode'] == 0) {
            $accessToken = $json['access_token'];
            return $accessToken;
        } else {
            Log::init(array(
                'log_path' => C('LOG_PATH')
            ));
            Log::write('获取accessToken失败');
            return null;
        }
    }

    private function _sendPost($url, $data)
    {
        $ch      = curl_init($url);
        $payload = json_encode($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    private function _sendGet($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    private function _getAllDepartmentList()
    {
        $accessToken = $this->_getAccessToken();
        if ($accessToken == null) {
            return '获取accessToken失败';
        }
        $url     = 'https://oapi.dingtalk.com/department/list?access_token=' . $accessToken;
        $content = $this->_sendGet($url);
        $json    = json_decode($content, true);

        if ($json['errcode'] == 0) {
            return $json['department'];
        } else {
            return array();
        }
    }

    public function getTechDepartmentList()
    {
        $departmentList     = $this->_getAllDepartmentList();
        $techDepartmentList = array();
        foreach ($departmentList as $department) {
            $techDepartmentList[] = $department;
        }
        return $techDepartmentList;
    }

    public function getTechUserList()
    {
        $techDepartmentList = $this->getTechDepartmentList();
        $techUserList       = array();
        foreach ($techDepartmentList as $techDepartment) {
            $userList     = $this->getDepartmentUserList($techDepartment['id']);
            $techUserList = array_merge($techUserList, $userList);
        }
        return $techUserList;
    }

    public function getDepartmentUserList($departmentId)
    {
        $accessToken = $this->_getAccessToken();
        if ($accessToken == null) {
            return '获取accessToken失败';
        }
        $url     = 'https://oapi.dingtalk.com/user/simplelist?access_token=' . $accessToken . '&department_id=' . $departmentId;
        $content = $this->_sendGet($url);
        $json    = json_decode($content, true);
        if ($json['errcode'] == 0) {
            return $json['userlist'];
        } else {
            return array();
        }
    }
}